package com.wclass.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wclass.entity.BaseAppUser;
import com.wclass.entity.BaseOrgan;
import com.wclass.entity.CouresOrder;
import com.wclass.entity.CourseInfo;
import com.wclass.mapper.CouresOrderMapper;
import com.wclass.service.IBaseAppUserService;
import com.wclass.service.IBaseOrganService;
import com.wclass.service.ICouresOrderService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wclass.service.ICourseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程订单 服务实现类
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-21
 */
@Service
public class CouresOrderServiceImpl extends ServiceImpl<CouresOrderMapper, CouresOrder> implements ICouresOrderService {

    @Autowired
    private IBaseOrganService iBaseOrganService;
    @Autowired
    private IBaseAppUserService iBaseAppUserService;
    @Autowired
    private ICourseInfoService iCourseInfoService;

    @Override
    public Page<Map<String, Object>> selectCourseSettleCountList(Page<Map<String, Object>> page, EntityWrapper<CouresOrder> ew) {
        return page.setRecords(this.baseMapper.selectCourseSettleCountList(page, ew));
    }

    /**
     * 获取学生列表
     * @param page
     * @param userId
     * @return
     */
    @Override
    public Page<Long> selectTeaStudentList(Page<Long> page, Long userId) {
        return page.setRecords(this.baseMapper.selectTeaStudentList(page, userId));
    }

    /**
     * 获取含有学习进度的订单课程
     * @param page
     * @param userId
     * @return
     */
    @Override
    public Page<Long> selectHasProcessList(Page<Long> page, Long userId) {
        return page.setRecords(this.baseMapper.selectHasProcessList(page, userId));
    }

    /**
     * 更新机构、教师学生数量
     * @param courseInfo
     * @param couresOrder
     * @param userId
     * @param type 0：添加，1：减少
     */
    @Override
    public void updateStudentNum(CourseInfo courseInfo, CouresOrder couresOrder, Long userId, Integer type){
        /*
         * 课程为机构课程时机构学生+1
         * 已购买该机构课程则学生数不变
         */
        BaseOrgan organ = iBaseOrganService.selectOne(new EntityWrapper<BaseOrgan>().eq("user_id", courseInfo.getCrtUserId()));
        if (organ != null) {
            //获取机构能创建课程的校长、主任、教师id
            EntityWrapper<BaseAppUser> userEw = new EntityWrapper<>();
            userEw.setSqlSelect("id");
            userEw.eq("institution_id", organ.getId()).le("rold_id", 3).eq("institution_invite_state", 2);
            List<Object> userIdList = iBaseAppUserService.selectObjs(userEw);

            //获取机构创建的课程id
            EntityWrapper<CourseInfo> infoEw = new EntityWrapper<>();
            infoEw.setSqlSelect("id");
            infoEw.eq("audit_state", 1).eq("state", 0).in("crt_user_id", userIdList);
            List<Object> courseIdList = iCourseInfoService.selectObjs(infoEw);

            //查看用户购买机构课程数量
            EntityWrapper<CouresOrder> orderEw = new EntityWrapper<>();
            orderEw.eq("pay_person_id", userId).in("course_info_id", courseIdList).eq("pay_state", 1);
            int count = this.selectCount(orderEw);
            if (count == 0) {
                BaseOrgan organRecord = new BaseOrgan();
                organRecord.setId(organ.getId());
                Integer stundentNum = organ.getStudentNum();
                if (type == 0)
                    ++stundentNum;
                else
                    --stundentNum;
                organRecord.setStudentNum(stundentNum);
                iBaseOrganService.updateById(organRecord);
            }
        }
        /*
         * 教师更新学生数量
         * 已购买教师课程不变
         */
        iBaseAppUserService.updateStudentNum(courseInfo.getUserId());
    }

}
